<?php

class Rsa {

	private $pubkey = "-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDEIyAlA5dzNrvV3bRWpYD/Rjtz
19p+b/bzed/xytUmlWIqGYLAr/2Exqq8s1Vok50IqbszNRmE2IkfA7sjjLpy2Qct
13NIvI3kviaHZS3Dc5Qb67zY+P/25+35NeXkev67z+gQHGsYC//dIjVpexfewZ7j
SJaHyu5EXbTyQ/THrwIDAQAB
-----END PUBLIC KEY-----";
	
	
	private $privkey = "-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQDEIyAlA5dzNrvV3bRWpYD/Rjtz19p+b/bzed/xytUmlWIqGYLA
r/2Exqq8s1Vok50IqbszNRmE2IkfA7sjjLpy2Qct13NIvI3kviaHZS3Dc5Qb67zY
+P/25+35NeXkev67z+gQHGsYC//dIjVpexfewZ7jSJaHyu5EXbTyQ/THrwIDAQAB
AoGBAJp3Q1CDNJojB+kVUnpOm3nvONExtLyQtv7nA6wr3pQlX4riTmwr7c/MmhhE
b8YNQsXYzjo03mUWF19ak94abZa2DcStFdmfj4ScFHlUBERh7d0jqtQxaKFaFQ+B
PHF1yeB7gqS1jvU3S5ULV1RWmm2xuc2q1HHbFpcAVN+AxeQhAkEA6JjGFFaVwMj+
ovPMXML/Z5D7jvLC4ApKYXD38Ooailez0wMMR4oiuxzSerRMy2dGW1u2E1Eh0pSP
Zh5db/QZXwJBANffOgMNvwjCNvN88eXaVrzYNlUkKRLAKWoUlFEcaYWKMADOsaS9
HRBnLdX+E13mlppqC8ymQjIug83ZuwRQ47ECQELxZWEJIoSf1Jm1rUWtYbC/C4ey
2t98VrgAuXt/BTB32WuL3wbf/Or5eqKSLeAE0z0MAjVBDoj69OKhLqdAC+UCQCxo
hZyfNU9t9LP+Xhz036cVq4e0Wqa61Uo3VZOiDqskFF/2JWFIbjQzpaBCFmXYy96U
Nj0xbqi4HPE5+1kV8uECQGqZ+JwVVXT+QQryHNAwwZF4O8muaflPB613C3h8H0yq
wVfxDlQ+bt6bt9dU7cZ5FoaUphYZhD7WvpQgNuq3vCk=
-----END RSA PRIVATE KEY-----";

    private $sdk_pubkey = "-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDQKFDQWLx8tLUQoqtNLht28fPh
BtCu0dfOF5qOJI/DVh8Jfv7axJV9Nx+UT/VTj8cIhd6SkFQnqxWlhEEWjSMPMjWQ
lj4RFqPKGkGGTP3e+/adnmK2LqMqNWf6l1zfvSORg6yUi+YU79r2fMT3Dt0OCl+9
exL/9kYtFUd/47RTOQIDAQAB
-----END PUBLIC KEY-----";


    private $sdk_privkey = "-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDQKFDQWLx8tLUQoqtNLht28fPhBtCu0dfOF5qOJI/DVh8Jfv7a
xJV9Nx+UT/VTj8cIhd6SkFQnqxWlhEEWjSMPMjWQlj4RFqPKGkGGTP3e+/adnmK2
LqMqNWf6l1zfvSORg6yUi+YU79r2fMT3Dt0OCl+9exL/9kYtFUd/47RTOQIDAQAB
AoGAAvdwH2gEV6qjofcPhewQTCOqnBxiwPsQnklL1JbEzb3ed14t96QxlTVB5/Uz
w9satQ5jW6de66nOhytZWh7szvYNl56NK5m9Alq0hBQ5qX8rkgmn90LUKf15JNEY
UGm+B2X2lFq2UPjc8nBkbq0AruXPExfq7xCL5n1o/CDGxAECQQDqckeFyG7Wgfgc
E0G2ZXdHmTEWX3V7u8rFb0VP92kWi641N4uDG/gJD7rQuxaFV96YY3cJkd8g80OX
jsqUSmf5AkEA40tS5GOAj//OyDh36f1YrPIyot9eoYUGLvm5mVA4JI21QvOT6wEE
dVfCKDyQqGTeulpQljmYepwViNQ+a6yVQQJBANovFm7j1HrfI8cFCM+1aCeC4tL+
bbiEUTYi0q+UAgHQZoTyN20B13ifYe2lX1UjLG5Hit2mGrBwlEP0yITvJgkCQEh/
tijzNAa6aZKjsFwKW0aO2mfpJ54NeDNzpCeq1r2SFccNOpky8eEb5OpAp0OPKRv3
wsyoAmLZdmT2jhJ6MgECQQCTub1Ifa1VVkg7UQti7rwml7NtXT/VLH5dhi6HOMMo
+Ut6xiHQQp/yEFQpu1KkjODGwZAHXHmUunBZji3EP5Gw
-----END RSA PRIVATE KEY-----";

	function __construct($myPrivateKey = '', $myPublicKey = '') {

		if(isset($myPrivateKey) && !empty($myPrivateKey)){
			$this->privkey = $myPrivateKey;
		}

		if(isset($myPublicKey) && !empty($myPublicKey)){
			$this->pubkey = $myPublicKey;
		}
		 
	}
	
	/**
	 * Des: RSA加密
	 */
	public function encrypt($data) {

		if (openssl_public_encrypt($data, $encrypted, $this->pubkey,OPENSSL_NO_PADDING))
			$data = base64_encode($encrypted);
		else
			$data = '';

		return $data;
	}
	
	/**
	 * DES: RSA解密
	 */
	public function decrypt($data) {
		if (openssl_private_decrypt(base64_decode($data), $decrypted, $this->privkey,OPENSSL_NO_PADDING))
			$data = $decrypted;
		else
			$data = '';

		return trim($data);
	}
	
	/**
	 * Des: Ios RSA加密
	 */
	public function iosEncrypt($data) {

		if (openssl_public_encrypt($data, $encrypted, $this->pubkey,OPENSSL_PKCS1_PADDING))
			$data = base64_encode($encrypted);
		else
			$data = '';

		return $data;
	}
	
	/**
	 * DES: Ios RSA解密
	 */
	public function iosDecrypt($data) {
		if (openssl_private_decrypt(base64_decode($data), $decrypted, $this->privkey,OPENSSL_PKCS1_PADDING))
			$data = $decrypted;
		else
			$data = '';

		return trim($data);
	}
	
	public function test(){
	}


    /**
     * Des: RSA加密
     */
    public function sdk_encrypt($data) {

        if (openssl_public_encrypt($data, $encrypted, $this->sdk_pubkey,OPENSSL_NO_PADDING))
            $data = base64_encode($encrypted);
        else
            $data = '';

        return $data;
    }

    //补齐128位
    public function noPaddingStr($str) {
        return str_pad($str, 128, " ", STR_PAD_RIGHT);
    }
}

